﻿/*
 * Managed ESL Suite
 * 
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The Initial Developer of the Original Code is
 * Diego Toro <dftoro@yahoo.com>
 * Portions created by the Initial Developer are Copyright (C)
 * the Initial Developer. All Rights Reserved.
 * 
 * Contributor: 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;

namespace Managed.Esl.Suite
{
  public class EventBase
  {
    #region Field

    protected ESLevent eslEvent;
    private string stCoreUUID;
    private int inEventCallingLineNumber;
    private DateTime dtEventDateGMT;
    private DateTime dtEventDateLocal;

    #endregion Field

    #region Property

    #region Minimum event information

    public string EventName
    {
      get { return eslEvent.GetHeader("Event-Name"); }
    }

    public string CoreUUID
    {
      get { return stCoreUUID; }
    }

    public DateTime EventDateLocal
    {
      get { return dtEventDateLocal; }
    }

    public DateTime EventDateGMT
    {
      get { return dtEventDateGMT; }
    }

    public string EventCallingFile
    {
      get { return eslEvent.GetHeader("Event-Calling-File"); }
    }

    public string EventCallingFunction
    {
      get { return eslEvent.GetHeader("Event-Calling-Function"); }
    }

    public int EventCallingLineNumber
    {
      get { return inEventCallingLineNumber; }
    }

    public ESLevent EslEvent
    {
      get { return eslEvent; }
    }

    #endregion Minimum event information

    #endregion Property

    #region Constructor

    public EventBase(ESLevent eslEvent)
    {
      if (eslEvent == null)
      {
        throw new ArgumentNullException("eslEvent");
      }

      this.eslEvent = eslEvent;

      LoadDefaultProperties();
    }

    public EventBase(string type, string subClassName)
    {
      this.eslEvent = new ESLevent(type, subClassName);

      if (this.eslEvent == null)
      {
        throw new Exception("Error, ESLEvent isn't created");
      }

      LoadDefaultProperties();
    }

    #endregion Constructor

    #region Public Member

    public override string ToString()
    {
      if (String.IsNullOrEmpty(eslEvent.SerializedString))
      {
        eslEvent.Serialize("");
      }

      return eslEvent.SerializedString;
    }

    public string GetHeader(string headerName)
    {
      return this.eslEvent.GetHeader(headerName);
    }

    public string GetBody()
    {
      return this.eslEvent.GetBody();
    }

    #endregion Public Member

    #region Private Member

    private void LoadDefaultProperties()
    {
      if (String.IsNullOrEmpty(this.eslEvent.SerializedString))
      {
        this.eslEvent.Serialize(String.Empty);
      }

      stCoreUUID = eslEvent.GetHeader("core-UUID");
      string stTemp = eslEvent.GetHeader("event-date-local");
      dtEventDateLocal = String.IsNullOrEmpty(stTemp) ? DateTime.Now : DateTime.Parse(Uri.UnescapeDataString(stTemp));

      stTemp = eslEvent.GetHeader("event-date-GMT");
      dtEventDateGMT = String.IsNullOrEmpty(stTemp) ? DateTime.Now : DateTime.Parse(Uri.UnescapeDataString(stTemp));

      stTemp = eslEvent.GetHeader("event-calling-line-number");
      inEventCallingLineNumber = String.IsNullOrEmpty(stTemp) ? -1 : int.Parse(stTemp);
    }

    #endregion Private Member
  }
}

